package com.huyanqi.front.rest.resource.impl;

import java.util.List;

import javax.annotation.Resource;

import com.huyanqi.core.RetdObj;
import com.huyanqi.core.util.MD5Tools;
import com.huyanqi.entity.User;
import com.huyanqi.front.rest.RespCode;
import com.huyanqi.front.rest.resource.UserListResource;
import com.huyanqi.front.rest.resp.UserListResp;
import com.huyanqi.front.rest.resp.UserSimpleResp;
import com.huyanqi.front.rest.service.UserRestService;

public class UserResourceImpl implements UserListResource{

	private UserRestService restService;
	
    @Resource(name="UserRestService")
	public void setRestService(UserRestService restService) {
		this.restService = restService;
	}
    public UserRestService getRestService() {
    	return restService;
    }

	@Override
	public UserListResp getUserList() {
		UserListResp listResp=new UserListResp();
		try {
			RetdObj<List<User>> retdObj=restService.getUserList();
			listResp.setCode(RespCode.valueOf(retdObj.getCode()));
			List<User> users=retdObj.getRealObj();
			listResp.setData(users);
		} catch (Exception e) {
			listResp.setCode(RespCode.EX_APP);
			e.printStackTrace();
		}
		return listResp;
	}

	@Override
	public UserSimpleResp regUser(String email,String nickname,String password) {
		UserSimpleResp simpleResp=new UserSimpleResp();
		User u=new User();
		u.setUEmail(email);
		u.setUNickname(nickname);
		u.setUPassword(password);
		try {
			RetdObj<User> retdObj=restService.regUser(u);
			simpleResp.setCode(RespCode.valueOf(retdObj.getCode()));
			simpleResp.setResult(retdObj.getRealObj());
		} catch (Exception e) {
			simpleResp.setCode(RespCode.EX_APP);
			e.printStackTrace();
		}
		return simpleResp;
	}
	@Override
	public UserSimpleResp loginUser(String email, String password,String isMd5) {
		UserSimpleResp simpleResp=new UserSimpleResp();
		User u = new User();
		u.setUEmail(email);
		if(isMd5.equals("false")){
			u.setUPassword(password);
		}else{
			u.setUPassword(MD5Tools.toMd5(password));
		}
		RetdObj<User> retdObj = restService.loginUser(u);
		simpleResp.setResult(retdObj.getRealObj());
		simpleResp.setCode(RespCode.valueOf(retdObj.getCode()));
		return simpleResp;
	}
	
	@Override
	public UserSimpleResp getUserByUid(Long uid) {
		UserSimpleResp usr = new UserSimpleResp();
		RetdObj<User> retd = restService.getUserByUid(uid);
		usr.setResult(retd.getRealObj());
		usr.setCode(RespCode.valueOf(retd.getCode()));
		return usr;
	}
	
	
}
